//
//  AppDelegate+extension.m
//  CoolTennisBall
//
//  Created by Coollang on 2017/8/4.
//  Copyright © 2017年 CoolLang. All rights reserved.
//

#import "AppDelegate+extension.h"
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKConnector/ShareSDKConnector.h>
#import <SMS_SDK/SMSSDK.h>

#import "WXApi.h"
#import <TencentOpenAPI/TencentApiInterface.h>
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>
#import <ShareSDK/ShareSDK+Base.h>

@implementation AppDelegate (extension)

#pragma mark - 配置ShareSDK
- (void)configureShareSDK {
    NSLog(@"sharesdk version: %@", [ShareSDK sdkVer]);
    
    [SMSSDK registerApp:@"dd71fe32ab10" withSecret:@"897179e8325592aeef628731018aced4"];
    [WXApi registerApp:@"wxfef357e50e890305"];
    
    [ShareSDK registerApp:@"e0d0d155f35c"
          activePlatforms:@[
                            @(SSDKPlatformTypeWechat),
                            @(SSDKPlatformTypeQQ),
                            @(SSDKPlatformTypeFacebook)
                            ] onImport:^(SSDKPlatformType platformType) {
                                switch (platformType){
                                    case SSDKPlatformTypeWechat:
                                        [ShareSDKConnector connectWeChat:[WXApi class]];
                                        break;
                                    case SSDKPlatformTypeQQ:
                                        [ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]];
                                        break;
                                    case SSDKPlatformTypeFacebook:
                                       
                                    default:
                                        break;
                                }
                            }
          onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo) {
              switch (platformType) {
                  case SSDKPlatformTypeWechat:
                      [appInfo SSDKSetupWeChatByAppId:@"wxfef357e50e890305"
                                            appSecret:@"422c33cbfcfb96a3beda317605b680bd"];
                      break;
                  case SSDKPlatformTypeQQ:
                      [appInfo SSDKSetupQQByAppId:@"1105042486" appKey:@"TUJu4qBKg0xPQEwB" authType:SSDKAuthTypeBoth];
                      break;
                  case SSDKPlatformTypeFacebook:
                      [appInfo SSDKSetupFacebookByApiKey:@"1420955631267078" appSecret:@"47853d658d771016e5359a6bdcd5337a" authType:SSDKAuthTypeBoth];
                      break;
                  default:
                      break;
              }
              
          }];
    
}

#pragma mark - 数据库迁移
- (void)migrationRealm
{
    // 在 [AppDelegate didFinishLaunchingWithOptions:] 中进行配置
    
    RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
    // 设置新的架构版本。这个版本号必须高于之前所用的版本号（如果您之前从未设置过架构版本，那么这个版本号设置为 0）
    config.schemaVersion = 15;
    
    // 设置闭包，这个闭包将会在打开低于上面所设置版本号的 Realm 数据库的时候被自动调用
    config.migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) {
        // 目前我们还未进行数据迁移，因此 oldSchemaVersion == 0
        if (oldSchemaVersion < 4) {
            // 什么都不要做！Realm 会自行检测新增和需要移除的属性，然后自动更新硬盘上的数据库架构
            [migration enumerateObjects:SportRecordItem.className block:^(RLMObject *oldObject, RLMObject *newObject) {
                newObject[@"shouldShield"] = @NO;
            }];
        }
        
        if (oldSchemaVersion < 7) {
            [migration enumerateObjects:User.className block:^(RLMObject *oldObject, RLMObject *newObject) {
                newObject[@"sportRecordItems"] = oldObject[@"SportRecordItems"];
            }];
        }
        
        if (oldSchemaVersion < 10) {
            [migration enumerateObjects:DeviceInfo.className block:^(RLMObject * _Nullable oldObject, RLMObject * _Nullable newObject) {
                newObject[@"oemType"] = @(OemTypeOrignal);
            }];
        }
        if (oldSchemaVersion < 14) {
            
        }
        // 什么都不要做！Realm 会自行检测新增和需要移除的属性，然后自动更新硬盘上的数据库架构
    };
    
    // 告诉 Realm 为默认的 Realm 数据库使用这个新的配置对象
    [RLMRealmConfiguration setDefaultConfiguration:config];
    
    // 现在我们已经告诉了 Realm 如何处理架构的变化，打开文件之后将会自动执行迁移
    [RLMRealm defaultRealm];
}

- (void)cleanRealmDataIfUpdateVersion
{
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *appCurVersion = infoDictionary[@"CFBundleShortVersionString"];
    
    if (![appCurVersion isEqualToString:[[NSUserDefaults standardUserDefaults] stringForKey:@"version"]]) {
        [DataModel setAutoLogin:NO];
        [DataModel saveAppVersion:appCurVersion];
        
        [[RLMRealm defaultRealm] beginWriteTransaction];
        [[RLMRealm defaultRealm] deleteAllObjects];
        [[RLMRealm defaultRealm] commitWriteTransaction];
        
        // 默认开启实时训练后台语音
        [DataModel setRealTimeSound:YES];
    }
}

@end
